package cn.yoyo.controller.adm;

import cn.yoyo.entity.OrderLine;
import cn.yoyo.entity.SysUser;
import cn.yoyo.service.OrderService;
import cn.yoyo.tools.GetUser;
import cn.yoyo.tools.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AdmOrderController {
    @Autowired
    OrderService orderService;

    public AdmOrderController() {
        System.out.println("asdaiusdjaoisjdalksdjalksdajd");
    }

    @RequestMapping("/api/adm/order/find/findOrderLines/{start}/{num}")
    public Map<String, Object> findOrderLines(@PathVariable("start") Integer start, @PathVariable("num") Integer num) {
        SysUser user = GetUser.getUser();
        if (user == null) {
            return RespJson.pack(401, "Token过期,请重新登录", null);
        }
        if (start < 1 && num < 1) {
            return RespJson.pack(500, "上传参数有误", null);
        }
        List<OrderLine> orders = orderService.findOrderLines(start, num);
        if (orders.size() == 0) {
            return RespJson.pack(500, "查询数据有误", null);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total", orderService.getTotal(new HashMap<>()));
        map.put("orders", orderService.findOrderLines(start, num));
        return RespJson.pack(200, "", map);
    }

    // 发货
    @RequestMapping("/api/adm/order/send")
    public Map<String, Object> send(@RequestBody Map<String, Object> map) {
        orderService.send(map);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("total", orderService.getTotal(new HashMap<>()));
        map1.put("orders", orderService.findOrderLines((Integer) map.get("start"), (Integer) map.get("num")));
        return RespJson.pack(200, "发货成功", map1);
    }

    @RequestMapping("/api/adm/order/findOrderByInfo")
    public Map<String, Object> findOrderByInfo(@RequestBody Map<String, Object> map) {
        Map<String, Object> map1 = new HashMap<>();
        map1.put("total", orderService.getTotal(map));
        map1.put("orders", orderService.findOrderByInfo(map));
        return RespJson.pack(200, "", map1);
    }

}
